package top.linyuxb.lycloud.gateway.starter.exception;

import lombok.Getter;

/**
 * Created by Intellij IDEA.
 *
 * @Author: linyuxb
 * @Date: 2024/8/16 17:46
 * @Desc:
 */
@Getter
public class ServerException extends RuntimeException {

    private int code;

    public ServerException() {
        super("系统内部错误");
    }

    public ServerException(String message) {
        super(message);
    }

    public ServerException(String message, int code) {
        super(message);
        this.code = code;
    }

    public ServerException(ResponseCodeEnum responseCode) {
        super(responseCode.getMessage());
        this.code = responseCode.getCode();
    }

    public static void checkAndThrow(Boolean expression, ResponseCodeEnum returnCode) {
        if (!expression) {
            throw buildWithCode(returnCode);
        }
    }

    public static ServerException buildWithCode(ResponseCodeEnum returnCode) {
        return new ServerException(returnCode);
    }

}
